package com.astn.bks.util;

import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;

public class JsonUtils {
	public static Gson gson;
	public static JsonParser parser;

	public static void initial() {
		GsonBuilder gBuilder = new GsonBuilder();
		gBuilder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
			SimpleDateFormat dateFormatYMD = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat dateFormatYMDHMS = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");

			@Override
			public Date deserialize(JsonElement arg0, Type arg1,
					JsonDeserializationContext arg2) throws JsonParseException {
				try {
					return dateFormatYMD.parse(arg0.getAsString());
				} catch (Exception e) {
					try {
						return dateFormatYMDHMS.parse(arg0.getAsString());
					} catch (Exception e2) {
						return null;
					}
				}
			}
		});
		gson = gBuilder.create();
		parser = new JsonParser();

	}
}
